<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = DB::table('users')->get();
         return view('admin.user.index',['users'=>$users]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.user.create');

    }

    /**
     * Store a newly created resourc
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // dd($_POST);
        $res = DB::table('users')->insert(
            ['phone' => $_POST['phone'],
            'username' =>  $_POST['username'],
            'password' =>$_POST['password'],
            'email' =>  $_POST['email'],
            'auth' =>  $_POST['auth']]
        );
        // dd($res);
        return redirect('admin/user');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $users = DB::table('users')->get();
         return view('admin.user.index',['users'=>$users]);
        // echo 555;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $users = DB::table('users')->where('id',$id)->first();
        // dd($users);
        return view('admin.user.edit',['users'=>$users]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // dd($_POST);
        $res = DB::table('users')
            ->where('id', $id)
            ->update(['phone' => $_POST['phone'],
            'username' =>  $_POST['username'],
            'password' =>$_POST['password'],
            'email' =>  $_POST['email'],
            'auth' =>  $_POST['auth']]
            );
            // dd($res);
            return redirect('admin/user');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = DB::table('users')->where('id',$id)->delete();
        return back();
    }
}
